package com.edushare.common.backup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Date;

import com.edushare.common.constants.ConfigPropertyName;
import com.edushare.common.global.SystemGlobals;
import com.edushare.common.util.SystemUtils;
import com.edushare.common.vo.SystemBackup;

public class SystemRestoreUtils {

	public static final String COMMAND_START = "/bin/sh -c \"";
	public static final String SPACE = " ";
	public static final String COMMAND_END = "\"";
	
	public static void createRestoreScript(SystemBackup object) throws FileNotFoundException {
		
//		*** ALL PATHS MUST BE ABSOLUTE PATH ***
		String mediaDirPath = SystemUtils.getMediaDirectoryPath(false);
		String currentDir = SystemUtils.getAbsolutePathFromRelative(object.getDirectoryPath());
		String dbUserName = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_USER);
		String dbHost = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_HOST);
		String dbPassword = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_PASSWORD);
		String dbPort = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_PORT);
		String dbName = SystemGlobals.getInstance().getConfigProperty(ConfigPropertyName.SYSTEM_DATABASE_NAME);
		
		File dir = new File(currentDir);
		File shell = new File(dir, SystemBackup.RESTORE_SCRIPT_FILE_NAME);
		PrintStream prn = new PrintStream(shell);
		
//		0. stop server
		prn.println(COMMAND_START + "asadmin stop-domain" + COMMAND_END);
		
//		1. rename existing media directory media-old
		prn.println(COMMAND_START + "mv"+ SPACE + mediaDirPath + SPACE +mediaDirPath+"-old-"+new Date().getTime() + COMMAND_END);
		
//		2. merge database
		prn.println(COMMAND_START + "cat"+
										SPACE + currentDir + "/" + SystemBackup.DB_BKP_FILE_PREFIX +".tar.bz.*" +
										SPACE + ">" +
										SPACE + currentDir + "/" + SystemBackup.DB_BKP_FILE_PREFIX +".tar.bz" + 
										COMMAND_END);
		
//		3. merge media zip
		prn.println(COMMAND_START + "cat"+
				SPACE + currentDir + "/" + SystemBackup.MEDIA_BKP_FILE_PREFIX+".tar.bz.*" +
				SPACE + ">" +
				SPACE + currentDir + "/" + SystemBackup.MEDIA_BKP_FILE_PREFIX+".tar.bz" + 
				COMMAND_END);
		
//		4. decompress the media file 
		prn.println(COMMAND_START + "cd" + SPACE + currentDir+";"+
				SPACE + "tar"+
				SPACE + "-xf" +
				SPACE + SystemBackup.MEDIA_BKP_FILE_PREFIX+".tar.bz" + 
				COMMAND_END);
		
//		5. move new media files
		prn.println(COMMAND_START + "mv"+ 
						SPACE + currentDir + "/" + SystemBackup.MEDIA_BKP_FILE_PREFIX + 
						SPACE + mediaDirPath + 
						COMMAND_END);
		
//		6. decompress database files
		prn.println(COMMAND_START + "cd" + SPACE + currentDir+";"+
				SPACE + "tar"+
				SPACE + "-xf" +
				SPACE + SystemBackup.DB_BKP_FILE_PREFIX+".tar.bz" + 
				COMMAND_END);
//		7. execute db script
		prn.println(COMMAND_START + "mysql" +
				SPACE + "-u"+ SPACE + dbUserName + 
				SPACE + "-p" + dbPassword + //no-space!
				SPACE + dbName +
				SPACE + "<" +
				SPACE + currentDir + "/" + SystemBackup.DB_BKP_FILE_PREFIX + 
				COMMAND_END);
		
//		8. start server		
		prn.println(COMMAND_START + "asadmin start-domain" + COMMAND_END);
		
//		9. delete zip files
		prn.println(COMMAND_START + "rm -rf" + SPACE + currentDir + "/" + SystemBackup.MEDIA_BKP_FILE_PREFIX+".tar.bz" + COMMAND_END);
		prn.println(COMMAND_START + "rm -rf" + SPACE + currentDir + "/" + SystemBackup.DB_BKP_FILE_PREFIX+".tar.bz" + COMMAND_END);
		
//		10. delete the SQL file
		prn.println(COMMAND_START + "rm -rf" + SPACE + currentDir + "/" + SystemBackup.DB_BKP_FILE_PREFIX + COMMAND_END);
		
		prn.close();
	}

}
